<?php

namespace App\Admin\Controllers\Audit;

use App\Admin\Actions\Audit\AuditRealNameAuthAction;
use App\Admin\Grid\NGrid;
use App\Models\AuditModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Filter;

class AuditRealNameAuthController extends AdminController
{
    protected $title = '实名认证';

    /**
     * grid
     * @return NGrid
     */
    protected function grid(): NGrid
    {
        $grid = new NGrid(new AuditModel);
        $grid->model()->leftJoin('shop_user', 'shop_user.user_id', 'audit.user_id')->orderByDesc('audit.id')->where([
            'type' => 'realNameAuth', 'status' => 0])->select(['audit.*', 'user_mobile']);
        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->equal('user_mobile', '手机号');
            });
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->like('real_name', '真实姓名');
            });
        });
        $grid->column('id', 'ID')->sortable();
        $grid->column('serial_number', __('l.serialNumber'));
        $grid->column('user_mobile', __('l.user_mobile'));
        $grid->column('realName', '真实姓名')->display(function () {
            return $this->content['realName'];
        });
        $grid->column('idNum', '身份证')->display(function () {
            return $this->content['idNum'];
        });
        $grid->column('authFileId', '身份证正面照')->display(function () {
            return view('image', ['id' => $this->id, 'image' => fileIdToUrl($this->content['authFileId'])]);
        });
        $grid->column('authReverseFileId', '身份证返面照')->display(function () {
            return view('image', ['id' => $this->id, 'image' => fileIdToUrl($this->content['authReverseFileId'])]);
        });
        $grid->column('authHandFileId', '手持身份证照')->display(function () {
            if (empty($this->content['authHandFileId'])) return null;
            else return view('image', ['id' => $this->id, 'image' => fileIdToUrl($this->content['authHandFileId'])]);
        });
        $grid->column('created_at', __('l.createdAt'));
        $grid->actions(function (Actions $actions) {
            $actions->disableEdit();
            $actions->disableDelete();
            $actions->disableView();
            $actions->add(new AuditRealNameAuthAction);
        });
        $grid->disableActions(false);
        return $grid;
    }
}
